/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package yuhuibear.poat.util;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * 
 * @author yuhuibear
 */
public class NameConvertorTest {

	public NameConvertorTest() {
	}

	@BeforeClass
	public static void setUpClass() throws Exception {
	}

	@AfterClass
	public static void tearDownClass() throws Exception {
	}

	@Before
	public void setUp() {
	}

	@After
	public void tearDown() {
	}

	/**
	 * Test of toTableName method, of class NameConvertor.
	 */
	@Test
	public void testToTableName_String() {
		System.out.println("String toTableName(String name)");
		String name = "User.NameId = Log.Id and Log2.test ='eiI''FI' and User.credite = 'aaa''b'";
		System.out.println(name);
		String expResult = "users.name_id = logs.id and log2s.test ='eiI''FI' and users.credite = 'aaa''b'";
		String result = NameConvertor.toTableName(name);
		System.out.println(result);
		assertEquals(expResult, result);
	}

	/**
	 * Test of toTableAttributes method, of class NameConvertor.
	 */
	@Test
	public void testToTableAttributes() {
		System.out.println("toTableAttributes");
		String name = " User.idTest = User.LogProfile sum'_= _aa..a' ";
		String expResult = "id_test = users.log_profile sum'_= _aa..a'";
		String result = NameConvertor.toTableAttributes(name);
		assertEquals(expResult, result);
	}

	/**
	 * Test of extractBeanName method, of class NameConvertor.
	 */
	@Test
	public void testExtractBeanName() {
		String exp = "SumLogs(User.id, Log.name, 'ase, Dogs.Id(T''ser.name))'Mtas),Ta.a";
		List<String> expResult = new LinkedList<String>();
		expResult.add("User");
		expResult.add("Log");
		expResult.add("Ta");
		List<String> result = NameConvertor.extractBeanName(exp);
		assertArrayEquals(expResult.toArray(), result.toArray());

		// exp = "select "
		// + "ps.*, e.name as name, s.spec as spec, e.type as type "
		// +
		// "from pic_paper_suites as ps , elements as e, element_subversions as s "
		// + "where ps.uid = s.uid and s.element_id = e.id";
		// expResult = new LinkedList<String>();
		// expResult.add("User");
		// expResult.add("Log");
		// expResult.add("Ta");
		// result = NameConvertor.extractBeanName(exp);
		// assertArrayEquals(expResult.toArray(), result.toArray());

	}

	/**
	 * Test of extractFullBeanAttributeName method, of class NameConvertor.
	 */
	@Test
	public void testExtractFullBeanAttributeName() {
		System.out.println("extractFullBeanAttributeName");
		String exp = "SumLogs(User.id, Log.name, 'ase, 'Dogs.Id'(Tser.na''me))'Mtas),Ta.a";
		String[] expResult = new String[] { "User.id", "Log.name", "Dogs.Id",
				"Ta.a" };
		List<String> result = NameConvertor.extractFullBeanAttributeName(exp);
		assertArrayEquals(expResult, result.toArray());
	}

	/**
	 * Test of toBeanName method, of class NameConvertor.
	 */
	@Test
	public void testToBeanName_String() {
		System.out.println("toBeanName");
		String name = "_atrb_of_bean_";
		String expResult = "AtrbOfBean";
		String result = NameConvertor.toBeanName(name);
		assertEquals(expResult, result);
	}

	@Test
	public void testExtractEntityName_String() throws Exception {
		System.out.println("testExtractEntityName_String");
		Set<String> expResult = new HashSet<String>();
		expResult.add("pic_papaer_circulate_librarys");
		expResult.add("pic_papers");
		expResult.add("dictionarys");
		expResult.add("Table_A");
		expResult.add("departments");

		String sql = "select factoryhas.pic_paper_id as pic_paper_id, factoryhas.quantity as totalquantity, (factoryhas.quantity-departmenthas.quantity) as factquantity, dic.name as type "
				+ "from "
				+ "("
				+ "(	"
				+ "select ppcl.pic_paper_id as pic_paper_id, sum(ppcl.quantity) as quantity from pic_papaer_circulate_librarys as ppcl where ppcl.place = 0 group by ppcl.pic_paper_id having sum(ppcl.quantity)>0"
				+ ") as factoryhas "
				+ "left outer join "
				+ "(	"
				+ "select ppcl.pic_paper_id as pic_paper_id, sum(ppcl.quantity) as quantity from pic_papaer_circulate_librarys as ppcl where ppcl.place = 1 or ppcl.place = 3 group by ppcl.pic_paper_id"
				+ ") as departmenthas  on departmenthas.pic_paper_id = factoryhas.pic_paper_id"
				+ "),"
				+ " pic_papers as pp, dictionarys as dic, "
				+ " ("
				+ "(select * from Table_A) as view_test "
				+ "outter join departments on departments.id = Table_A.id"
				+ ") "
				+ "where pp.id=factoryhas.pic_paper_id  and pp.type=dic.id;";

		Set<String> result = NameConvertor.extractEntityName(sql);
		assertEquals(expResult, result);

		sql = "select view.productionitermid as productionitermid, employee_name_by_id(br.borrower_id) as borrower, sum(view.broken) as broken " +
				"from " +
					"(" +
						"select view.productionitermid as productionitermid, view.broken as broken " +
							"from " +
							"(" +
								"(" +
									"select pi.id as productionitermid, sum(qa.broken) as broken "
									+ "from production_iterms as pi, quality_assurences as qa,  work_recods as wr where pi.id=wr.production_iterm_id and wr.id=qa.work_record_id group by pi.id" +
								") union (" +
									"select pi.id as productionitermid, sum(oqa.broken) as broken " +
								    	"from production_iterms as pi, out_quality_assurences as oqa, outter_processings as op where pi.id=op.production_iterm_id and op.id=oqa.outter_processing_id group by pi.id" +
								    	")" +
						   ") as view" +
				   ") as view " +
				   "where pi.uid not in (select uid from plan_need) group by view.productionitermid; ";
		expResult = new HashSet<String>();
		expResult.add("production_iterms");
		expResult.add("quality_assurences");
		expResult.add("work_recods");
		expResult.add("out_quality_assurences");
		expResult.add("outter_processings");
		expResult.add("plan_need");
		expResult.add("employee_name_by_id");
		result = NameConvertor.extractEntityName(sql);
		assertEquals(expResult, result);

		expResult = new HashSet<String>();
		expResult.add("factoryhas");
		sql = "select factoryhas(aaaab.3r423)";
		result = NameConvertor.extractEntityName(sql);
		assertEquals(expResult, result);

	}
}